package cn.homework.dao.impl;

import cn.homework.dao.OrdersDao;
import cn.homework.domain.Customers;
import cn.homework.domain.Notice;
import cn.homework.domain.OrderDetails;
import cn.homework.domain.Orders;
import cn.homework.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ACT
 */
public class OrderDaoImpl implements OrdersDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public void addOrderToDatabase(Orders orders, String name) {
        String sql = "insert into orders values(?, ?, ?, ?, ?, ?, ?, ?)";
        String id = String.valueOf(randomNum(19));
        String user = queryCustomerByName(name).getName();
        String time = currentTime();
        Float sum = price();
        template.update(sql, id, orders.getPayment(), orders.getAddress(), orders.getEmail(),
                user, time, sum, orders.getMessage());
        emptyShoppingCart();
    }

    @Override
    public List<Orders> findAllOrders(String name) {
        String sql = "select * from orders where user = ?";
        List<Orders> ordersList = template.query(sql, new BeanPropertyRowMapper<>(Orders.class), name);
        return ordersList;
    }

    @Override
    public List<Orders> findAllOrdersReveal() {
        String sql = "select * from orders";
        List<Orders> list = template.query(sql, new BeanPropertyRowMapper<>(Orders.class));
        return list;
    }

    @Override
    public void delOrderById(String id) {
        String sql = "delete from orders where id = ?";
        template.update(sql, id);
    }

    @Override
    public List<Orders> findOrderByCondition(Map<String, String[]> map) {
        String sql = "select * from orders  where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
        //遍历map
        Set<String> keySet = map.keySet();
        //定义参数的集合
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {
            //排除条件信息
            if ("demand".equals(key)) {
                continue;
            }

            //获取values
            String values = map.get(key)[0];
            //判断是否有值
            if (values != null && !"".equals(values)) {
                //有值
                sb.append(" and " + key + " like ? ");
                params.add("%" + values + "%");
            }
        }
        sql = sb.toString();
        List<Orders> list = template.query(sql, new BeanPropertyRowMapper<>(Orders.class), params.toArray());
        return list;
    }

    private String currentTime() {
        Date data = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String string = sdf.format(data);
        return string;
    }

    private Long randomNum(int factor){
        return new Double((Math.random() + 1) * Math.pow(10, factor - 1)).longValue();
    }

    private Customers queryCustomerByName(String name) {
        String sql = "select * from customers where name = ?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Customers.class), name);
    }

    private Float price() {
        String sql = "select * from orderdetails";
        List<OrderDetails> orderDetails = template.query(sql, new BeanPropertyRowMapper<>(OrderDetails.class));
        float sum = 0;
        for (OrderDetails orderDetail : orderDetails) {
            sum = orderDetail.getPrice() * orderDetail.getNumber() + sum;
        }
        return sum;
    }

    private void emptyShoppingCart() {
        String sql = "delete from orderdetails";
        template.update(sql);
    }
}
